//  This product includes software developed by Denim Group, Ltd. (http://www.denimgroup.com/)
// All use and distribution of the Denim Group developed software is subject to Version 2.0
// of the Apache License (http://www.apache.org/licenses/LICENSE-2.0).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AppScan.DefectLogger
{
	public partial class DefectSystemConfigForm : Form
	{
		private ConnectionForm _connForm;

		private class viewOrderComparer : System.Collections.IComparer
		{
			#region IComparer Members

			public int Compare(object x, object y)
			{
				DataGridViewRow row1 = x as DataGridViewRow;
				DataGridViewRow row2 = y as DataGridViewRow;

				if (row1 == null && row2 == null){ return 0; }
				if (row1 == null){ return -1; }
				if (row2 == null) { return 1; }

				int row1Val = 999;
				int row2Val = 999;
				int.TryParse(row1.Cells["ViewOrderCol"].Value.ToString(), out row1Val);
				int.TryParse(row2.Cells["ViewOrderCol"].Value.ToString(), out row2Val);

				if (row1Val == row2Val) { return 0; }
				if (row1Val > row2Val) { return 1; }
				return -1;
			}

			#endregion
		}
	
		DefectLoggerBase _extension = null;

		public DefectSystemConfigForm(ConnectionForm connForm, DefectLoggerBase extension)
		{
            _extension = extension;
            InitializeComponent();
			_connForm = connForm;
			panelConfig.Controls.Add(_connForm);
			_connForm.Init(this.FieldsTable, extension);
            LoadConfig(extension.Config);
		}

		private void LoadConfig(DefectSystemConfig config)
		{

			_connForm.SetConfig(config);

			FieldsTable.Rows.Clear();
			foreach (Field field in config.Fields)
			{
				FieldsTable.Rows.Add(
					field.DisplayName,
					field.Visible,
					field.Required,
					field.FType.ToString(),
					string.Join(",",field.PossibleValues.ToArray()),
					field.DefaultValue,
					field.ViewOrder,
					field.Name);
			}
		}

		private void CancelBtn_Click(object sender, EventArgs e)
		{
			Close();
		}

		private DefectSystemConfig CreateConfig()
		{
			// Create a new configuration item
			DefectSystemConfig newConfig = new DefectSystemConfig();

			_connForm.GetConfig(newConfig);

			newConfig.Fields = new List<Field>();

			// sort by View Order, assumes last column is View Order
			FieldsTable.Sort(new viewOrderComparer());

			foreach (DataGridViewRow row in FieldsTable.Rows)
			{

				Field field = new Field();
				field.DisplayName = row.Cells[0].Value.ToString();
				field.Visible = (bool)row.Cells[1].Value;
				field.Required		= (bool)row.Cells[2].Value;
				
				field.FType			= (Field.FieldType)Enum.Parse(typeof(Field.FieldType), row.Cells[3].Value.ToString());
				field.PossibleValues= (row.Cells[4].Value == null) ? 
										new List<string>() : 
										new List<string>(row.Cells[4].Value.ToString().Split(','));
				field.DefaultValue	= (row.Cells[5].Value == null) ? 
										string.Empty : 
										row.Cells[5].Value.ToString();

				int viewOrder;
				if (int.TryParse(row.Cells["ViewOrderCol"].Value.ToString(), out viewOrder))
				{
					field.ViewOrder = viewOrder;
				}
				else
				{
					field.ViewOrder = 0;
				}
				field.Name = row.Cells["FieldNameCol"].Value.ToString();
				
				newConfig.Fields.Add(field);
			}

			return newConfig;
		}

		private void SaveBtn_Click(object sender, EventArgs e)
		{
			_extension.SaveConfig(CreateConfig());

			Close();
		}


		private void RefreshBtn_Click(object sender, EventArgs e)
		{

		}

		private void ProjectDropDown_SelectedIndexChanged(object sender, EventArgs e)
		{

		}

		private void QCConfigForm_FormClosed(object sender, FormClosedEventArgs e)
		{
		}

		private void ImportBtn_Click(object sender, EventArgs e)
		{
			DialogResult res = openFileDialog1.ShowDialog(this);
			if (res == DialogResult.OK)
			{
				string xml = System.IO.File.ReadAllText(openFileDialog1.FileName);
				DefectSystemConfig config = DefectSystemConfig.FromXml(xml);
				if (config == null)
				{
					MessageBox.Show("Failed to import configuration from specified file");
					return;
				}

				LoadConfig(config);
			}
		}

		private void ExportBtn_Click(object sender, EventArgs e)
		{
			DialogResult res = saveFileDialog1.ShowDialog(this);
			if (res == DialogResult.OK)
			{
				System.IO.File.WriteAllText(saveFileDialog1.FileName, CreateConfig().ToXml());
			}
		}

	}
}